package com.gxa.sso.service.impl;

import com.gxa.sso.service.SmsService;
import com.gxa.sso.utils.SendSms;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service("smsService")
@Transactional(rollbackFor = Throwable.class)
public class SmsServiceImpl implements SmsService {

    /**
     * redis操作缓存验证码
     */
    @Resource
    private RedisTemplate redisTemplate;

    // redis存储的key设为常量   key:login_sms:13511112222  value:123456
    public static final String LOGIN_SMS="login_sms:";

    @Override
    public void sendLoginSms(String phone) {

        //一键登录 有没有账号都可以发送短信
        //发送登录  没有注册账号就不行
        //发送注册短信 注册了就不行

        //1.生成验证码(用工具类的静态方法调用)
        String msgCode = SendSms.getMsgCode();

        //生成的验证码打印在控制台用于作弊获取
        System.out.println("========msgCode=======:"+msgCode);

        //2.服务器存储验证码并设置失效时间为30分钟
        redisTemplate.opsForValue().set(LOGIN_SMS+phone,msgCode,30, TimeUnit.MINUTES);

        //3.调用工具类发送短信给用户
        String templateParam2 = "{\"code\":123456}";
        // String templateParam = "{\"getCode\":\""+msgCode+"\", \"storeName\":\"淘宝直营店\", \"address\":\"阿里巴巴上海分公司\",\"detail\":\"{1}号仓{2}件\"}";
      //  SendSms.sendSms(phone,templateCode,templateParam2);

        // 4. 短信接口返回的数据
        //SendSmsResponse response = sendSms(mobile,templateCode,templateParam);
        // System.out.println("短信接口返回的数据----------------");
        //System.out.println("Code=" + response.getCode());         // OK:成功
        // System.out.println("Message=" + response.getMessage());
        // System.out.println("RequestId=" + response.getRequestId());
        // System.out.println("BizId=" + response.getBizId());

    }
}